/**
 * handle gateway manager web page requests.
 */
var gatewayTable = require("../Redis/gatewayTable.js");
var accountTable = require("../Redis/accountTable.js");
var redis = require("../Redis/redis.js");

exports.getGateways = function(req, res) {
	"use strict";

	var context = {};
	redis.setClient(context);

	var gateways = [];
	var count = 0;
	var cursor = 0;
	var current = 0;
	if (req.query) {
		if (req.query.cursor) {
			cursor = req.query.cursor;
		}

		if (req.query.current) {
			current = +req.query.current;
		}
	}

	context.beginMulti();
	gatewayTable.count(context, function(err, reply) {
		if (err) {
			console.error("count error:" + err);
		} else {
			count = +reply;
		}
	});

	gatewayTable.scan(context, cursor, 16, function(err, replies) {
		if (err) {
			console.error("scan error:" + err);
		}

		if (replies) {
			cursor = +replies[0];
			var gids = replies[1];
			if (gids) {
				var length = gids.length;
				current += length;
				for (var x = 0; x < length; ++x) {
					gateways.push("" + gids[x]);
				}
			}
		}
	});

	context.endMulti(function(err) {

		if (err) {
			console.error("getControllers database error:" + err);
		}

		var navigatorHeader = "Total:" + count + ", current:" + current;
		var nextPage = "/getGateways";
		if (cursor !== 0) {
			nextPage += ("?cursor=" + cursor + "&current=" + current);
		}
		res.render("gatewayMgr", {
			title : "Gateway manager",
			"navigatorHeader" : navigatorHeader,
			"nextPage" : nextPage,
			"gateways" : gateways,
			footer : "All gateways in database",
			header: "All gateways in database"
		});
	});

};


exports.loadGateway = function(req, res) {
	"use strict";
	var context = {};
	redis.setClient(context);
	
	var gid = req.query.gid;
	var result = {};
	var gateway = {};
	gateway.controllerCount = 0;
	gateway.groupCount = 0;
	
	context.beginMulti();
	gatewayTable.count_c(context, gid, function(err, reply){
		if (err) {
			result.err = "" + err;
		} else {
			gateway.controllerCount = +reply;
		}
	});
	var fields = [ "devId", "panId", "description", "account", "logintime", "name" ];
	gatewayTable.loadFields(context, gid, fields, function(err, replies) {
		if (err) {
			result.err = "" + err;
		}
		
		if (replies) {
			gateway.devId = "" + replies[0];
			gateway.panId = "" + replies[1];
			gateway.description = "" + replies[2];
			if (replies[3]) {
				gateway.account = "" + replies[3];
			} else {
				gateway.account = "";
			}
			
			gateway.logintime = "" + replies[4];
			if (replies[5]) {
				gateway.name = "" + replies[5];
			} else {
				gateway.name = "";
			}
			
			result.gateway = gateway;
		}
	});
	
	context.endMulti(function(err){
		if (err) {
			console.error(err);
			result.err = "" + err;
		}
		res.send(JSON.stringify(result));
	});
};

exports.editGateway = function(req, res) {
	"use strict";
	var gwId = req.query.gwId;
	
	var context = redis.getContext();
	gatewayTable.load(context, gwId, function(err, replies){
		if (err) {
			res.send("" + err);
		} else {
			var gateway = {};
			for(var k in replies) {
				if (replies.hasOwnProperty(k)) {
					gateway[k] = "" + replies[k];
				}
			}
			
			if (!gateway.name) {
				gateway.name = "";
			}
			
			if (!gateway.description) {
				gateway.description = "";
			}
			
			if (!gateway.account) {
				gateway.account = "";
			}
			
			gateway.title = "Edit gateway";
			
			res.render("gatewayEdit", gateway);
		}
	});
};

exports.checkGatewayAccount = function(req, res) {
	"use strcit";
	var account = req.query["textinput-account"];
	
	var context = redis.getContext();
	accountTable.loadFields(context, account, ["id"], function(err, replies){
		if (err) {
			res.send("" + err);
		} else {
			if (!replies[0]) {
				res.send("account not exist");
			} else {
				res.send("true");
			}
		}
	});
};

exports._saveGateway = function(req, res) {
	"use strict";
	var body = req.body;
	var gwId = body["textinput-devId"];
	var name = body["textinput-name"];
	var description = body["textinput-description"];
	var account = body["textinput-account"];
	
	var context = redis.getContext();
	gatewayTable.setFields(context, gwId, ["name", name, "account", account, "description", description], function(err){
		if (err) {
			res.send("" + err);
		} else {
			res.send("ok");
		}
	});
};

